package com.eddie.tenalgorithm.kmp;

/**
 * @author Eddie
 * @date 2022/08/16 10:30
 **/
public class ViolenceMatch {
    public static void main(String[] args) {
        //测试暴力匹配算法
        String str1 = "你好Eddie你好 好你Eddie你好die你好好你Eddie你好die你好";
        String str2 = "你好Eddie你好";
        int i = violenceMatch(str1, str2);
        System.out.println(i);
    }

    public static int violenceMatch(String str1, String str2) {
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();
        int s1Len = s1.length;
        int s2Len = s2.length;

        //i索引指向s1
        int i = 0;
        //j索引指向s2
        int j = 0;
        //保证匹配不越界
        while (i < s1Len && j < s2Len) {
            //匹配成功
            if (s1[i] == s2[j]) {
                i++;
                j++;
            } else {
                i = i - (j - 1);
                j = 0;
            }
        }

        //判断是否匹配成功
        if (j == s2Len) {
            return i - j;
        } else {
            return -1;
        }
    }
}
